/**
 * @(#) ReqVacationMediator.as
 *
 * @date		2009. 03. 06.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	복리후생 - 선택적복리후생 신청 - Mediator
 * 				선택적 복리후생 신청 Presentation Logic을 관리 
 */
package com.skt.common_hr.welfare.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.welfare.WelfareFacade;
	import com.skt.common_hr.welfare.events.AccountSaveEvent;
	import com.skt.common_hr.welfare.model.BankAccountProxy;
	import com.skt.common_hr.welfare.model.vo.BankAccountVO;
	import com.skt.common_hr.welfare.view.components.BankAccountView;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class BankAccountMediator extends CommonMediator
	{
		public static const NAME:String = "BankAccountMediator"; 

		public var baseDate:Date;	

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function BankAccountMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			trace("SUSS initView");
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(BankAccountView.CANCEL_ACCOUNT_CONTROL, onCancel);
 			view.addEventListener(AccountSaveEvent.Account, onAccountSave);
 			//view.addEventListener(ReqWorkTimeView.DEL_RECORD, onDelRecord);
 			accountDataSelect();
 		}
 		
 		
 		private function accountDataSelect():void{
 			var proxyData:BankAccountProxy = facade.retrieveProxy(BankAccountProxy.NAME) as BankAccountProxy;
 			proxyData.load(Variables.myPernr);
 		}
 		
 		private function onAccountSave(event:AccountSaveEvent):void{
			//view.removeEventListener(AccountSaveEvent.Account, onAccountSave);
			
			var proxyBankCode:BankAccountProxy = facade.retrieveProxy(BankAccountProxy.NAME) as BankAccountProxy;
 			proxyBankCode.save(event.pernr, event.accvo);
		}
 		
 		private function onCancel(event:Event):void
 		{
 			facade.removeMediator(BankAccountMediator.NAME);
 			PopUpManager.removePopUp(view);
 			trace("onCancel suss");
 		}
 		
 		/****
 		 * 콤보박스 array의 데이터 위치 return;
 		 * **/
 		private function searchComboIndex(arr:ArrayCollection, val:String):int{
 			for(var i:int = 0; i < arr.length; i++){
 				if(arr[i].CODE == val){
 					return i;
 				}
 			}
 			return -1;
 		}
 		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					WelfareFacade.LOAD_BANK_ACCOUNT_COMPLETE,
					WelfareFacade.LOAD_ACCOUNT_SAVE_COMPLETE,
					WelfareFacade.LOAD_ACCOUNT_SAVE_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case WelfareFacade.LOAD_BANK_ACCOUNT_COMPLETE:
					view.bankCodeList = (note.getBody() as BankAccountProxy).bankCodeList;
					view.defaultBank = (note.getBody() as BankAccountProxy).defaultBank;
					view.additionBank = (note.getBody() as BankAccountProxy).additionBank;
					if(view.additionBank != null){
						view.cbBack.selectedIndex = searchComboIndex(view.bankCodeList, view.additionBank.BANKL);
					}
					else {
						view.additionBank = new BankAccountVO();
						view.additionBank.EMFTX = Variables.myName; 
					}
					break;
				case WelfareFacade.LOAD_ACCOUNT_SAVE_COMPLETE:
					onCancel(null);
					facade.sendNotification(WelfareFacade.RELOAD_ACCOUNT_INFO, this);
					break;
				case WelfareFacade.LOAD_ACCOUNT_SAVE_FAILED:
					var proxy:BankAccountProxy = BankAccountProxy(note.getBody());
					if(proxy.retCode == 4) {
						CommonAlert.showMessage(commonFacade.bundle, "ALERT_SAME_ACCOUNT"); 
						onCancel(null);
					}
					else {
						CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as BankAccountProxy).msgStr);
					}
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():BankAccountView
        {
            return viewComponent as BankAccountView;
        }
	}
}